﻿using FairyGUI;
using System.Collections;

/// <summary>
/// 创建房间
/// </summary>
public class UIHome_CreateRoom : UIPopupWindow {

    static UIHome_CreateRoom _inst;

    public UIHome_CreateRoom()
    {
        _inst = this;

        // 加载界面
        base.Init(UIPackage.CreateObject("主界面", "创建房间").asCom);

        // 初始化关闭按钮
        GButton closeBtn = uiComp.GetChild("关闭按钮").asButton;
        closeBtn.onClick.Add(() => {
            _Close();
        });

        // 单选控制
        Controller roundCtrl = uiComp.GetController("局数控制");
        Controller laiZiCtrl = uiComp.GetController("赖子数控制");
		Controller bombCtrl = uiComp.GetController("炸弹数控制");

        // 初始化创建房间按钮按钮
        GButton okBtn = uiComp.GetChild("创建房间按钮").asButton;
        okBtn.onClick.Add(() => {
            DebugInfo.Log("创建房间按钮被点击");

            int round = (roundCtrl.selectedIndex == 0) ? 10 : 20;
            int nLaizi = (laiZiCtrl.selectedIndex == 0) ? 3 : 5;
			int nBomb = 3;
			switch(bombCtrl.selectedIndex) {
			case 0:
				nBomb = 3;
				break;
			case 1:
				nBomb = 4;
				break;
			case 2:
				nBomb = 5;
				break;
			}

            Hashtable reqArgs = new Hashtable();
            reqArgs["uid"] = DataKernel.inst.userInfo.uid;
            reqArgs["round"] = round;
            reqArgs["anyCardNum"] = nLaizi;
            reqArgs["gameType"] = 3;
			reqArgs["bombNum"] = nBomb;
            UIRootController.inst.ShowMask(true);
            NetIOController.inst.SendMessage(new IOMessage(IOCmds.CMSG_CREATE_ROOM, IOStates.STATE_OK, new HashtableEx(reqArgs)));
        });
    }

    void _Close()
    {
        DebugInfo.Log("关闭创建房间");

        AudioController.inst.PlayUIClickEffect();
        uiComp.RemoveFromParent();
        uiComp.Dispose();
        base.Close();
    }

    /// <summary>
    /// 弹出窗口
    /// </summary>
    public static void Popup()
    {
        DebugInfo.Log("打开创建房间");
        UIRootController.inst.PopupWindow(new UIHome_CreateRoom());
    }

    /// <summary>
    /// 关闭窗口
    /// </summary>
    public static void CloseWindow()
    {
        if (_inst != null)
        {
            _inst._Close();
        }
    }
}
